﻿using System.Collections.Generic;
using Obsidian.Action;
using Obsidian.Utils;

/*============================
 * @author [#AUTHOR#]
 * @created [#CREATED#]
 * @desc 
* ============================ */

namespace [#ApiNS#]
{
    /// <summary>
    /// [#ClsName#] 接口API
    /// </summary>
    public class [#ClsName#]API :[#ClsBaseApi#]
    {
		private const string API_GET = "[#apiPrefix#].[#sClsName#].get";
		private const string API_LIST = "[#apiPrefix#].[#sClsName#].list";
		private const string API_ADD = "[#apiPrefix#].[#sClsName#].add";
		private const string API_UPDATE = "[#apiPrefix#].[#sClsName#].update";
		private const string API_DELETE = "[#apiPrefix#].[#sClsName#].delete";
		//private string API_SETABLE = "[#apiPrefix#].[#sClsName#].setable";
		
		private const string ENTITY_STR = "[#sClsName#]"; //[请求]主参数名称
        private const string ENTITY_RES = "[#sClsName#]s"; //[返回]主参数名称
		private const string ID_STR = "[#IdAlias#]";
		private const string RESULT_STR = "result";
		private const string QUERY_STR = "qry";
		
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="[#IdAlias#]">请求参数</param>
        ///     [#IdAlias#]      {[#IdSysType#]}   	ID
        /// <returns></returns>
        ///     [#sClsName#]  {Entity}
        public ApiResponse Get([#IdSysType#] [#IdAlias#])
        {
            ActionRequest req = new ActionRequest(API_GET);

            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add(ID_STR, [#IdAlias#]);
            req.AddParam(ENTITY_STR, dict);

            ApiResponse apiRes = this.Execute(req);
            if (!apiRes.IsSucceed)
                return apiRes;

            ActionResponse res = req.Response;
            Entity eo = GetEntity(res, ENTITY_RES);
            apiRes.Set(ENTITY_STR, eo);
            return apiRes;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="qry">请求参数</param>
        /// <returns></returns>
        ///     [#sClsName#]s  {EntitySet}
        public ApiResponse List(Entity qry)
        {
            ActionRequest req = new ActionRequest(API_LIST);
            Dictionary<string, object> dict = qry.ToDictionary();
            req.AddParam(QUERY_STR, dict);

            ApiResponse apiRes = this.Execute(req);
            if (!apiRes.IsSucceed)
                return apiRes;

            ActionResponse res = req.Response;
            EntitySet es = GetEntitySet(res, ENTITY_RES);
            apiRes.Set(ENTITY_RES, es);

            return apiRes;
        }

		/// <summary>
        /// 添加
        /// </summary>
        /// <param name="prms">请求参数</param>
        /// <param name="sessionKey"></param>
        /// <returns>返回结果集参数</returns>
        ///     [#sClsName#]  {Entity}
        public ApiResponse Add(Entity prms, string sessionKey)
        {
            ActionRequest req = new ActionRequest(API_ADD); //接口名称

            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict = prms.ToDictionary();		//组装请求参数
            req.AddParam(ENTITY_STR, dict); //“绑定”请求参数

            ApiResponse apiRes = this.Execute(req, sessionKey); //调用接口服务（如果需要用户判断则增加sessionKey）
            if (!apiRes.IsSucceed)	//判断调用是否正常
                return apiRes;

            ActionResponse res = req.Response;
            Entity eo = GetEntity(res, ENTITY_RES);   //获取调用结果
            apiRes.Set(ENTITY_STR, eo); //绑定”调用结果参数
            return apiRes;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="prms">请求参数</param>
        /// <param name="sessionKey"></param>
        /// <returns>返回结果集参数</returns>
        ///     [#sClsName#]  {Entity}
        public ApiResponse Update(Entity prms, string sessionKey)
        {
            ActionRequest req = new ActionRequest(API_UPDATE);

            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict = prms.ToDictionary();
            req.AddParam(ENTITY_STR, dict);

            ApiResponse apiRes = this.Execute(req, sessionKey);
            if (!apiRes.IsSucceed)
                return apiRes;

            ActionResponse res = req.Response;
            Entity eo = GetEntity(res, ENTITY_RES);
            apiRes.Set(ENTITY_STR, eo);
            return apiRes;
        }
		
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="[#IdAlias#]"></param>
        /// <param name="sessionKey"></param>
        ///     [#IdAlias#]
        ///     sessionKey      {string}
        /// <returns></returns>
        ///     result      {Entity}
        ///         code    {[#IdSysType#]}       执行代码
        ///         msg     {string}    提示信息
        public ApiResponse Delete([#IdSysType#] [#IdAlias#], string sessionKey)
        {
            ActionRequest req = new ActionRequest(API_DELETE);

            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add(ID_STR, [#IdAlias#]);
            req.AddParam(ENTITY_STR, dict);

            ApiResponse apiRes = this.Execute(req, sessionKey);
            if (!apiRes.IsSucceed)
                return apiRes;

            ActionResponse res = req.Response;
            Entity eo = GetEntity(res, RESULT_STR);
            apiRes.Set(RESULT_STR, eo);
            return apiRes;
        }
		
    }
}
